//
//  FLLFindViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/18.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLFindViewController.h"
#import "FLLMacro.h"
#import "FLLRelatedDishTableViewCell.h"
#import "FLLRelatedDishModel.h"
#import "RelatedDishData.h"
#import "RelatedDishSecData.h"
#import "ReletedDishTags_Info.h"
#import "FLLMakeFoodViewController.h"

@interface FLLFindViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *findTableView;
@property (nonatomic, strong) FLLRelatedDishModel *findModel;
@property (nonatomic, strong) NSMutableArray<RelatedDishSecData *> *dataSource;
@property (nonatomic, assign) BOOL idLoading;
@property (nonatomic, assign) NSInteger pageCount;
@end

@implementation FLLFindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"为你找到";
    [self createPOSTRequest];
    [self createFindTableView];
}
- (void)createPOSTRequest
{

    NSString *bodyString = [NSString stringWithFormat:@"page=%zd&methodName=SearchDishes&size=20&keyword=%@&version=4.40&",_pageCount, _keyString];
    [ZWYNetTool POST:@"http://api.izhangchu.com" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        if (result == nil) {
            NSLog(@"我是空的");
        }
        
        if (_idLoading == NO) {
            [self.dataSource removeAllObjects];
            _pageCount = 1;
            
        } else {
        
            _pageCount++;
        }
        self.findModel = [FLLRelatedDishModel modelWithDic:result];
        
        if (_idLoading == NO) {
            self.dataSource = (NSMutableArray *)self.findModel.data.data;
        } else {
        
            [self.dataSource addObjectsFromArray:(NSMutableArray *)self.findModel.data.data];
            
        }
        
        [self.findTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)createFindTableView
{

    self.findTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_findTableView];
    self.findTableView.delegate = self;
    self.findTableView.dataSource = self;
    [self.findTableView registerNib:[UINib nibWithNibName:@"FLLRelatedDishTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"relatedDishTableCell"];
    
    // 刷新
    self.findTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _idLoading = NO;
        [self createPOSTRequest];
        [self.findTableView.mj_header endRefreshing];
        
    }];
    // 加载
    self.findTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _idLoading = YES;
        [self createPOSTRequest];
        [self.findTableView.mj_footer endRefreshing];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLRelatedDishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"relatedDishTableCell"];
    cell.titleLabel.text = _dataSource[indexPath.row].title;
    cell.contentLabel.text = _dataSource[indexPath.row].description_Data;
    cell.levelLabel.text = [NSString stringWithFormat:@"难易度:%@", _dataSource[indexPath.row].hard_level];
    cell.tasteLabel.text = [NSString stringWithFormat:@"口味: %@", _dataSource[indexPath.row].taste];
    cell.timeLabel.text = [NSString stringWithFormat:@"烹饪时间: %@", _dataSource[indexPath.row].cooking_time];
    
    
    cell.titleLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    cell.contentLabel.font = [UIFont systemFontOfSize:13 *kWidthScale];
    cell.contentLabel.numberOfLines = 2;
    cell.levelLabel.font = [UIFont systemFontOfSize:12 * kWidthScale];
    cell.tasteLabel.font = [UIFont systemFontOfSize:12 * kWidthScale];
    cell.timeLabel.font = [UIFont systemFontOfSize:12 * kWidthScale];
    
    [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_dataSource[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    // 关键字高亮
    NSString *highLightString = self.keyString;
    NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:cell.titleLabel.text];
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:15 * kWidthScale], NSForegroundColorAttributeName : FLLOrangeColor};
    NSRange range = [cell.titleLabel.text rangeOfString:highLightString];
    [attribut addAttributes:dic range:range];
    cell.titleLabel.attributedText = attribut;

    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    return SCREEN_WIDTH * 0.4;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *idString = _dataSource[indexPath.row].dishes_id;
    FLLMakeFoodViewController *makeVC = [[FLLMakeFoodViewController alloc] init];
    makeVC.idString = idString;
    [self.navigationController pushViewController:makeVC animated:YES];
    
}
- (void)setKeyString:(NSString *)keyString
{

    _keyString = keyString;
    [self.findTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
